package com.example.wangning.mycarofcountry.findcar.brand;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class FindCarHotAdapter extends BaseExpandableListAdapter {
    private List<FindHotCarList> findHotCarLists;
    private Context context;
    private VolleySingleton volleySingleton;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    private int count = 0;


    public FindCarHotAdapter(Context c) {
        count++;
        context = c;
        volleySingleton = VolleySingleton.getVolleyIntance();
        inflater = LayoutInflater.from(c);
        imageLoader = volleySingleton.getImageLoader();
    }

    public void addData(List<FindHotCarList> f) {
        if (findHotCarLists == null) {
            findHotCarLists = new ArrayList<>();
        }
        findHotCarLists.clear();
        findHotCarLists.addAll(f);
//        notifyDataSetChanged();
        notifyDataSetInvalidated();
    }

    private <T> int getListSize(List<T> objects) {
        return objects != null && objects.size() > 0 ? objects.size() : 0;
    }

    private <T> T isNotObject(List<T> objects, int position) {
        return objects != null && objects.size() > 0 ? objects.get(position) : null;
    }

    @Override
    public int getGroupCount() {
        return findHotCarLists != null && findHotCarLists.size() > 0 ? findHotCarLists.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<FindHotCarHotseries> findHotCarHotseriesList = findHotCarLists.get(groupPosition).getHotseries();
        if (findHotCarHotseriesList != null && findHotCarHotseriesList.size() > 0) {
            Log.i("getChildrenCount", findHotCarHotseriesList.size() + "");
        }
        return getListSize(findHotCarHotseriesList);
    }

    @Override
    public Object getGroup(int groupPosition) {
        return isNotObject(findHotCarLists, groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<FindHotCarHotseries> findHotCarHotseriesList = findHotCarLists.get(groupPosition).getHotseries();
        return isNotObject(findHotCarHotseriesList, childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewGroupHolder viewGroupHolder;
        if (convertView == null) {
        convertView = inflater.inflate(R.layout.findcar_hotcar_item, null);
        viewGroupHolder = new ViewGroupHolder(convertView);
            convertView.setTag(viewGroupHolder);
        } else {
            viewGroupHolder = (ViewGroupHolder) convertView.getTag();
        }
        convertView.setClickable(true);
        FindHotCarList hotCar = (FindHotCarList) getGroup(groupPosition);
        viewGroupHolder.hotCar_TypeTv.setText(hotCar.getLevelname());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        convertView = inflater.inflate(R.layout.findcar_hotcar_title_item, null);
        viewHolder = new ViewHolder(convertView);
        FindHotCarHotseries hotseries = (FindHotCarHotseries) getChild(groupPosition, childPosition);
        if (hotseries != null) {
            viewHolder.hotCar_CarName_Tv.setText(hotseries.getSeriesname());
            viewHolder.hotCar_CarMoney_Tv.setText(hotseries.getPrice());
            viewHolder.hotCar_Iv.setDefaultImageResId(R.mipmap.icon);
            viewHolder.hotCar_Iv.setErrorImageResId(R.mipmap.icon);
            viewHolder.hotCar_Iv.setImageUrl(hotseries.getImg(), imageLoader);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class ViewHolder {
        public ViewHolder(View itemView) {
            hotCar_CarName_Tv = (TextView) itemView.findViewById(R.id.hotCar_CarName_Tv);
            hotCar_CarMoney_Tv = (TextView) itemView.findViewById(R.id.hotCar_CarMoney_Tv);
            hotCar_Iv = (NetworkImageView) itemView.findViewById(R.id.hotCar_Iv);
        }

        private TextView hotCar_CarName_Tv, hotCar_CarMoney_Tv;
        private NetworkImageView hotCar_Iv;
    }

    class ViewGroupHolder {
        public ViewGroupHolder(View itemGroupView) {
            hotCar_TypeTv = (TextView) itemGroupView.findViewById(R.id.hotCar_TypeTv);
        }

        private TextView hotCar_TypeTv;
    }
}
